#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project ：turbo 
@File    ：init.py
@Author  ：赵加响
@Date    ：2023/5/22 11:33 
@Desc    ：使用init命令初始化DB，这些DB为系统运行的基础
"""
from django.core.management import BaseCommand

from apps.accounts.models import SystemSettings, User
from apps.codetable.models import CodeTableHead, CodeTableDetail
from apps.rbac.models import Operate, Menu, MenuOperate, Role
from turbo import settings
from utils.system_settings.constant import PASSWORD_VALID_DAYS, PASSWORD_CHANGE_REMINDER_DAYS, INITIAL_PASSWORD, \
    DISABLE_PASSWORD_DUPLICATION_TIMES, PASSWORD_REGEX


class Command(BaseCommand):
    help = 'Initialize DB Data'

    def handle(self, *args, **options):
        self.stdout.write(self.style.SUCCESS('Initialize System Settings DB.'))
        SystemSettings.objects.get_or_create(code=PASSWORD_VALID_DAYS,
                                             defaults={'value': '365', 'content': '密码有效天数', 'value_type': 'int'})
        SystemSettings.objects.get_or_create(code=PASSWORD_CHANGE_REMINDER_DAYS,
                                             defaults={'value': '7', 'content': '密码修改提醒天数', 'value_type': 'int'})
        SystemSettings.objects.get_or_create(code=INITIAL_PASSWORD,
                                             defaults={'value': '123456', 'content': '默认初始密码', 'value_type': 'str'})
        SystemSettings.objects.get_or_create(code=DISABLE_PASSWORD_DUPLICATION_TIMES,
                                             defaults={'value': '', 'content': '禁止密码重复次数', 'value_type': 'int'})
        SystemSettings.objects.get_or_create(code=PASSWORD_REGEX,
                                             defaults={
                                                 'value': '',
                                                 'content': '密码规则正则表达式', 'value_type': 'str'})

        self.stdout.write(self.style.SUCCESS('Initialize Operates DB.'))
        inquiry, created = Operate.objects.get_or_create(code='INQUIRY', defaults={'name': '查询', 'name_en': 'Inquiry'})
        create, created = Operate.objects.get_or_create(code='CREATE', defaults={'name': '新建', 'name_en': 'Create'})
        update, created = Operate.objects.get_or_create(code='UPDATE', defaults={'name': '更新', 'name_en': 'Update'})
        delete, created = Operate.objects.get_or_create(code='DELETE', defaults={'name': '删除', 'name_en': 'Delete'})
        authorize, created = Operate.objects.get_or_create(code='AUTHORIZE',
                                                           defaults={'name': '授权', 'name_en': 'Authorize'})
        assigning, created = Operate.objects.get_or_create(code='ASSIGNING',
                                                           defaults={'name': '分配', 'name_en': 'Assigning'})
        reset, created = Operate.objects.get_or_create(code='RESET', defaults={'name': '数据重置', 'name_en': 'Reset'})
        release, created = Operate.objects.get_or_create(code='RELEASE', defaults={'name': '生效', 'name_en': 'Release'})
        close, created = Operate.objects.get_or_create(code='CLOSE', defaults={'name': '关闭', 'name_en': 'Close'})

        self.stdout.write(self.style.SUCCESS('Initialize Menus DB.'))
        # 主页设定为失效状态
        Menu.objects.get_or_create(code='HOME-1000',
                                   defaults={'name': '主页', 'name_en': 'Home', 'content': '前端主页菜单', 'is_active': False})
        rbac, created = Menu.objects.get_or_create(code='RBAC-1000',
                                                   defaults={'name': '权限管理', 'name_en': 'Permission Management',
                                                             'content': '前端权限管理'})
        account, created = Menu.objects.get_or_create(code='RBAC-1010',
                                                      defaults={'name': '用户管理', 'name_en': 'User Management',
                                                                'content': '前端用户管理', 'parent': rbac})

        role, created = Menu.objects.get_or_create(code='RBAC-1020',
                                                   defaults={'name': '角色管理', 'name_en': 'Role Management',
                                                             'content': '前端角色管理', 'parent': rbac})

        system, created = Menu.objects.get_or_create(code='SYSTEM-1000',
                                                     defaults={'name': '系统配置', 'name_en': 'System Setting',
                                                               'content': '前端系统配置'})
        parameter, created = Menu.objects.get_or_create(code='SYSTEM-1010',
                                                        defaults={'name': '系统参数', 'name_en': 'System Parameters',
                                                                  'content': '前端系统参数', 'parent': system})

        self.stdout.write(self.style.SUCCESS('Initialize MenuOperates DB.'))

        MenuOperate.objects.get_or_create(url='/account/list/', method='GET', menu=account,
                                          defaults={'operate': inquiry})
        MenuOperate.objects.get_or_create(url='/account/list/', method='POST', menu=account,
                                          defaults={'operate': create})
        MenuOperate.objects.get_or_create(url='/account/list/', method='PUT', menu=account,
                                          defaults={'operate': update})
        MenuOperate.objects.get_or_create(url='/account/list/', method='DELETE', menu=account,
                                          defaults={'operate': delete})
        MenuOperate.objects.get_or_create(url='/account/reset_password/', method='POST', menu=account,
                                          defaults={'operate': reset})
        MenuOperate.objects.get_or_create(url='/rbac/role/', method='GET', menu=role, defaults={'operate': inquiry})
        MenuOperate.objects.get_or_create(url='/rbac/role/', method='POST', menu=role, defaults={'operate': create})
        MenuOperate.objects.get_or_create(url='/rbac/role/', method='PUT', menu=role, defaults={'operate': update})
        MenuOperate.objects.get_or_create(url='/rbac/role/', method='DELETE', menu=role, defaults={'operate': delete})
        MenuOperate.objects.get_or_create(url='/rbac/role_menu_authorize/', method='POST', menu=role,
                                          defaults={'operate': authorize})
        MenuOperate.objects.get_or_create(url='/rbac/role_assign_user/', method='POST', menu=role,
                                          defaults={'operate': assigning})
        MenuOperate.objects.get_or_create(url='/system_settings/list/', method='GET', menu=parameter,
                                          defaults={'operate': inquiry})
        MenuOperate.objects.get_or_create(url='/system_settings/list/', method='PUT', menu=parameter,
                                          defaults={'operate': update})

        self.stdout.write(self.style.SUCCESS('Initialize Roles DB.'))
        role, created = Role.objects.get_or_create(code='admin',
                                                   defaults={'name_zh': '管理员', 'name_en': 'Administrator',
                                                             'is_admin': True})
        if created:
            self.stdout.write(self.style.SUCCESS('Create role (code: admin, administrator: true)'))

        self.stdout.write(self.style.SUCCESS('Initialize Users DB.'))
        user, created = User.objects.get_or_create(username='admin',
                                                   defaults={'name': '管理员', 'is_superuser': True, 'is_staff': True,
                                                             'is_active': True, 'init_status': False,
                                                             'user_type': 'A', 'invalid_date': '2049-12-31'})

        if created:
            user.roles.set(Role.objects.filter(code__exact='admin'))
            user.set_password('123456')
            user.save()
            self.stdout.write(
                self.style.SUCCESS('Create user (username: admin, password: 123456, role: admin, superuser: true)'))
